home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / octa209s.zip / octave-2.09 / libs / pnm / bitio.c next >
C/C++ Source or Header  |  1997-01-31  |  2KB  |  106 lines

  1. /*
  2. *******************************************************************************
  3. ** Bitstream I/O                                                             **
  4. ** (c) 1997, Klaus Gebhardt                                                  **
  5. *******************************************************************************
  6. */
  7.  
  8. #include "bitio.h"
  9.  
  10. struct bitstream
  11. {
  12.   FILE *f;                      /* bytestream */
  13.   unsigned long bitbuf;         /* bit buffer */
  14.   int nbitbuf;                  /* number of bits in 'bitbuf' */
  15.   char mode;
  16. };
  17.  
  18. #define Mask(n)        ((1<<(n))-1)
  19.  
  20. #define BitPut(b,ul,n)    ((b)->bitbuf = (((b)->bitbuf<<(n))    \
  21.                     |((ul)&Mask(n))),    \
  22.             (b)->nbitbuf += (n))
  23.  
  24. #define BitGet(b,n)    (((b)->bitbuf>>((b)->nbitbuf-=(n))) & Mask(n))
  25.  
  26.  
  27. struct bitstream *bitinit(FILE *f, const char *mode)
  28. {
  29.   struct bitstream *ans = (struct bitstream *)0;
  30.  
  31.   if(!f || !mode || !*mode)                   return ans;
  32.   if(strcmp(mode, "r") && strcmp(mode, "w"))  return ans;
  33.  
  34.   ans = (struct bitstream *)calloc(1, sizeof(struct bitstream));
  35.   if(ans)
  36.     {
  37.       ans->f = f;
  38.       ans->mode = *mode;
  39.     }
  40.  
  41.   return ans;
  42. }
  43.  
  44.  
  45. int bitfini(struct bitstream *b)
  46. {
  47.   int nbyte = 0;
  48.  
  49.   if(!b)  return -1;
  50.  
  51.   if(b->mode == 'w')
  52.     {
  53.       if ((b->nbitbuf < 0) || (b->nbitbuf >= 8))  return -1;
  54.  
  55.       if(b->nbitbuf)
  56.     {
  57.       char    c;
  58.  
  59.       BitPut(b, 0, (long)8-(b->nbitbuf));
  60.       c = (char) BitGet(b, (long)8);
  61.       if(putc(c, b->f) == EOF)  return -1;
  62.       nbyte++;
  63.     }
  64.     }
  65.  
  66.   free(b);
  67.   return nbyte;
  68. }
  69.  
  70. int bitread(struct bitstream *b, UINT nbits, UINT *val)
  71. {
  72.   int nbyte = 0;
  73.   int c;
  74.  
  75.   if(!b)  return -1;
  76.  
  77.   while (b->nbitbuf < nbits)
  78.     {
  79.       if((c = getc(b->f)) == EOF)  return -1;
  80.       nbyte++;
  81.       BitPut(b, c, (long) 8);
  82.     }
  83.  
  84.   *val = BitGet(b, nbits);
  85.   return nbyte;
  86. }
  87.  
  88. int bitwrite(struct bitstream *b, UINT nbits, UINT val)
  89. {
  90.   int nbyte = 0;
  91.   char c;
  92.  
  93.   if(!b)  return -1;
  94.  
  95.   BitPut(b, val, nbits);
  96.  
  97.   while (b->nbitbuf >= 8)
  98.     {
  99.       c = (char) BitGet(b, (long)8);
  100.       if(putc(c, b->f) == EOF)  return -1;
  101.       nbyte++;
  102.     }
  103.  
  104.   return nbyte;
  105. }
  106.